package com.zjdiepu.www.commpent.mapper.yw;

import java.math.BigDecimal;
import java.math.RoundingMode;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.stereotype.Component;

import com.zjdiepu.www.base.core.AbstractYwBusinessLoan;
import com.zjdiepu.www.base.core.BaseMapper;
import com.zjdiepu.www.base.enums.status.LoanEditStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.common.util.NumberUtil;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.dao.base.DpBaseDataDictionaryMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.system.IBaseSysParamsService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.dto.SimpleDataDictionary;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoan;
import com.zjdiepu.www.util.CachedEnums;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 项目名称：dpqc-parent-manager-information <br/> 
 * 类名称：AbstractmVoMapper <br/>  
 * 类描述： 贷款信息<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年4月26日 下午1:33:37  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年4月26日 下午1:33:37   <br/>  
 * 修改备注：    <br/> 
 * @update 2018-03-05
 * @author xiongxiaotun
 * @describe 存储量不参与计算，使用过程量计算。注：因为舍入问题，页面（存储量）计算出的结果可能有点出入，需要与客户解释<br/>
 * 解决方案:{@link AbstractYwBusinessLoanVoNewMapper}替代
 * @version V1.0   <br/>
 */
@Component
@Deprecated
public class AbstractYwBusinessLoanVoMapper extends BaseMapper<AbstractYwBusinessLoan>{
	@Resource
	private RedisObjectClient redisObjectClient;
	@Autowired
	private IBaseSysParamsService baseSysParamsService;
	@Autowired
	private ICachedDictionaryService cachedDictionaryService;
	@Autowired
	private DpBaseDataDictionaryMapper dpBaseDataDictionaryMapper;
	@Override
	public void mapper(RedisConnection redisConnection,AbstractYwBusinessLoan m) {
		if(null == m) {
			return;
		}
		Integer loanEditStatus = m.getLoanEditStatus();
		m.setLoanEditStatusDisplay((String)CachedEnums.getValue(loanEditStatus, LoanEditStatus.class));
		
		String loanType = m.getLoanType();
		SimpleDataDictionary loanTypeDic = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), loanType, DataDicType.LOAN_TYPE.getCode());
		m.setLoanTypeDisplay(loanTypeDic == null ? "" : loanTypeDic.getName());
		
		String loanBank = m.getLoanBank();
		SimpleDataDictionary loanBankDic = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), loanBank, DataDicType.LOAN_BANK.getCode());
		m.setLoanBankDisplay(loanBankDic == null ? "" : loanBankDic.getName());
		
		Integer loanMonth = m.getLoanMonth();
		SimpleDataDictionary loanMonthDic = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), loanMonth == null ? "" : loanMonth.toString(), DataDicType.LOAN_MONTH.getCode());
		m.setLoanMonthDisplay(loanMonthDic == null ? "" : loanMonthDic.getName());
		
		String ccol1 = m.getCcol1();
		SimpleDataDictionary ccol1Display = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), ccol1, DataDicType.INT_TYPE.getCode());
		m.setCcol1Display(ccol1Display == null ? "" : ccol1Display.getName());
		
		String rebateType = m.getRebateType();
		SimpleDataDictionary rebateTypeDic = redisObjectClient.hGet(redisConnection, SimpleDataDictionary.class.getSimpleName(), rebateType, DataDicType.REBATE_TYPE.getCode());
		m.setRebateTypeDisplay(rebateTypeDic == null ? null : rebateTypeDic.getName());
		
		Double bankRate = m.getBankRate();
		bankRate = m.getBankRate() == null ? 0 : bankRate;
		m.setBankRate(bankRate);

		YwBusinessLoan ywBusinessLoan = new YwBusinessLoan();
		BeanUtils.copyProperties(m, ywBusinessLoan);
		if("04".equals(loanType)){
			if("ICBC".equals(loanBank)) {
				calculationPx(ywBusinessLoan);
			} else if("TJICBC".equals(loanBank)) {
				calculationTjPx(ywBusinessLoan); //天津平息
			}
		} else if("02".equals(loanType)) {
			SimpleDataDictionary intType = cachedDictionaryService.getCachedDataDic(ywBusinessLoan.getCcol1(), DataDicType.INT_TYPE);
			if(null == intType) {
				ExceptionHelper.thow("999","没有配置取整模式");
			}
			String id = intType.getId();
			DpBaseDataDictionary dpBaseDataDictionary = dpBaseDataDictionaryMapper.selectByPrimaryKey(id);
			String addedValue2 = dpBaseDataDictionary == null ? "0" : dpBaseDataDictionary.getAddedValue2();
			if("ICBC".equals(loanBank)) {
				calculationGx(ywBusinessLoan, intType, addedValue2);
			} else if("TJICBC".equals(loanBank)) {
				calculationTjGx(ywBusinessLoan, intType, addedValue2);
			}
		} else if("05".equals(loanType)) {
			calculationZgYhIntResult(ywBusinessLoan);
		}
		
		m.setQyhkcj(ywBusinessLoan.getQyhkcj());
		m.setZhkcj(ywBusinessLoan.getZhkcj());
		m.setSfbl(ywBusinessLoan.getSfbl());
		m.setHjAmount(ywBusinessLoan.getHjAmount());
		m.setFqbl(ywBusinessLoan.getFqbl());
	}
	
	
	//计算中国银行
	public void calculationZgYh(AbstractYwBusinessLoan ywBusinessLoan){
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 2, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//二手车评估价(四舍五入获取真实输入)
		double carEstimatePrice = ywBusinessLoan.getCarEstimatePrice() == null ? 0 : ywBusinessLoan.getCarEstimatePrice();
		double K1 = NumberUtil.getBigDecimalHalfUp(carEstimatePrice, 2, 2);
		ywBusinessLoan.setCarEstimatePrice(K1);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 2, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//试算公司利率(四舍五入获取真实输入)
		String ccol2 = ywBusinessLoan.getCcol2();
		double cool2_ = StringUtils.isBlank(ccol2) ? 0 : Double.parseDouble(ccol2);
		ywBusinessLoan.setCcol2(new BigDecimal(cool2_).setScale(6, RoundingMode.HALF_UP).toString());
		
		//签约公司利率(只入不舍)
		double L3 = NumberUtil.getBigDecimalHalfUp(cool2_, 6, 6);
		ywBusinessLoan.setCompanyRate(L3);
		
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		//签约贷款额(只舍不入)		
		double N2 = NumberUtil.getBigDecimalUp(N0 * (1 + L3 - L0), 2, 8);
		//签约总还款额(只舍不入)
		double M1 = NumberUtil.getBigDecimalUp(N0 * (1 + L3), 2, 8);
		
		//签约每月还款额(只舍不入)
//		double R1 = NumberUtil.getBigDecimalUp(M1 / T, 2, -1);
		//签约首月还款额(只舍不入)
//		double R2 = NumberUtil.getBigDecimalUp(M1 - (T - 1) * R1, 2, 2);
		
		//签约每月本金还款额(只舍不入)
		double D1 = NumberUtil.getBigDecimalUp(N0 / T, 2, -1);
		//签约首月本金还款额(只舍不入)
		double D2 = NumberUtil.getBigDecimalUp(N0 - (T - 1) * D1, 2, 2);
		
		//签约手续费还款额1
		double S1 = NumberUtil.getBigDecimalUp(N0 * L3, 2, 8);
		//签约每月手续费还款额(只舍不入)
		double D3 = NumberUtil.getBigDecimalUp(S1 / T, 2, -1);
		//签约首月手续费还款额(只舍不入)
		double D4 = NumberUtil.getBigDecimalUp(S1 - (T - 1) * D3, 2, 8);
		
		//签约手续费还款额2
		double S2 = NumberUtil.getBigDecimalUp(N0 * L0, 2, 8);
		//签约每月手续费还款额(只舍不入)
		double D5 = NumberUtil.getBigDecimalUp(S2 / T, 2, -1);
		//签约首月手续费还款额(只舍不入)
		double D6 = NumberUtil.getBigDecimalUp(S2 - (T - 1) * D5, 2, 8);
		//银行利息
		double A = NumberUtil.getBigDecimalUp(N2 * L0, 2, 8);
		//按揭服务费
		double X = NumberUtil.getBigDecimalUp(N2 - N0, 2, 2);
		//银行利息
		ywBusinessLoan.setBankRateAmount(A);
		
		//签约贷款额(只舍不入)
		ywBusinessLoan.setSignLoanAmount(N2);
		//签约总还款额(只舍不入)
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		
		//签约每月还款额 amountMonthly
		ywBusinessLoan.setAmountMonthly(NumberUtil.getBigDecimalUp(D1 + D3, 2, 2));
		//签约首月还款额  firstMonthTotalAmount
		ywBusinessLoan.setFirstMonthTotalAmount(NumberUtil.getBigDecimalUp(D2 + D4, 2, 2));
		
		//签约每月本金还款额 monthlyPrincipalAmount
		ywBusinessLoan.setMonthlyPrincipalAmount(D1);
		//签约首月本金还款额  firstMonthAmount
		ywBusinessLoan.setFirstMonthAmount(D2);
		
		//签约手续费还款额1 stagesFee
		ywBusinessLoan.setStagesFee(S1);
		//签约每月手续费还款额1 monthlyFee
		ywBusinessLoan.setMonthlyFee(D3);
		//签约首月手续费还款额1 firstMonthFee
		ywBusinessLoan.setFirstMonthFee(D4);
		
		//签约手续费还款额1 stagesFee2
		ywBusinessLoan.setStagesFee2(S2);
		//签约每月手续费还款额1 monthlyFee2
		ywBusinessLoan.setMonthlyFee2(D5);
		//签约首月手续费还款额1 firstMonthFee2
		ywBusinessLoan.setFirstMonthFee2(D6);
		
		double E = 0;
		double F = 0;
		//新车
		if(K0 == 0.0) {
			//ExceptionHelper.thow("999", "二手车估价不能为0");
			E = 0;
			F = 0;
		} else {
			E = NumberUtil.getBigDecimalUp((M1 / K0), 6, -1);
			F = NumberUtil.getBigDecimalUp((N2 / K0), 6, -1);
		}
		ywBusinessLoan.setZhkcj(E);
		ywBusinessLoan.setQyhkcj(F);
		//按揭服务费
		ywBusinessLoan.setServicePrice(X);
		
		//首付和首付比例
		double firstPaymentAmount = NumberUtil.getBigDecimalUp(K0 - N0, 2, 2);
		ywBusinessLoan.setFirstPaymentAmount(firstPaymentAmount);
		ywBusinessLoan.setSfbl(NumberUtil.getBigDecimalUp(firstPaymentAmount / K0, 6, -1));
	}
		
	//计算高息
	public void calculationGx(AbstractYwBusinessLoan ywBusinessLoan, SimpleDataDictionary intType, String addedValue2) {
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 2, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//二手车评估价(四舍五入获取真实输入)
		double carEstimatePrice = ywBusinessLoan.getCarEstimatePrice() == null ? 0 : ywBusinessLoan.getCarEstimatePrice();
		double K1 = NumberUtil.getBigDecimalHalfUp(carEstimatePrice, 2, 2);
		ywBusinessLoan.setCarEstimatePrice(K1);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 2, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//试算公司利率(四舍五入获取真实输入)
		String ccol2 = ywBusinessLoan.getCcol2();
		double cool2_ = StringUtils.isBlank(ccol2) ? 0 : Double.parseDouble(ccol2);
		ywBusinessLoan.setCcol2(new BigDecimal(cool2_).setScale(6, RoundingMode.HALF_UP).toString());
		
		//试算公司利率/合计费率(四舍五入获取真实输入)
		//@update 2018-02-27 原“试算公司利率（L1）”改名为“合计费率（L1）”
		double L1 = cool2_;
		
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		double M0 = N0 * (1 + L1);
		double N1 = (M0 / (1 + L0));
		
		String addedValue1 = intType.getAddedValue1();//取整模式
		if(StringUtils.isBlank(addedValue1)) {
			ExceptionHelper.thow("999","取整模式附加值1必须为一个数字");
		}
		int intValue = Integer.parseInt(addedValue1);
		boolean up = "1".equals(addedValue2); //向上取整
		BigDecimal N1_ = new BigDecimal(N1).setScale(2, RoundingMode.HALF_UP);
		BigDecimal nIntValue_ = N1_.divide(new BigDecimal(intValue));
		int nIntValue = nIntValue_.setScale(0, RoundingMode.DOWN).intValue();
		double additional =  N1_.compareTo(new BigDecimal(nIntValue * intValue)) > 0 ? (up ? intValue : 0) : 0;
		double N2 = NumberUtil.getBigDecimalDown((nIntValue * intValue) + additional, 0, 2); //[舍去小数点][intValue进位]
		
		//试算公司利率2
		double L2 = N2 * (1 + L0) / N0 - 1;
		//签约公司利率(只入不舍)
		double L3 = NumberUtil.getBigDecimalUp(L2, 6, -1);
		ywBusinessLoan.setCompanyRate(L3);
		//试算签约贷款额
		double M1 = NumberUtil.getBigDecimalDown(N2 * (1 + L0), 2, 8);
		//按揭服务费(只舍不入)
		double X = NumberUtil.getBigDecimalDown(N2 - N0, 2, 2);
		//银行利息(只舍不入)
		double A = NumberUtil.getBigDecimalDown(N2 * L0, 2, 8);
		//试算月还款
		double B0 = M1 / T;
		//签约月还款(取整 只入不舍)
		double B1 = NumberUtil.getBigDecimalUp(B0, 0, -1);
		double C1 = NumberUtil.getBigDecimalDown(N2 * L0, 2, 8);
		double C2 = C1;
		double E = 0;
		double F = 0;
		//新车
		if(K0 == 0.0) {
			//ExceptionHelper.thow("999", "二手车估价不能为0");
			E = 0;
			F = 0;
		} else  {
			E = NumberUtil.getBigDecimalUp((M1 / K0), 6, -1);
			F = NumberUtil.getBigDecimalUp((N2 / K0), 6, -1);
		}

		//签约贷款额
		ywBusinessLoan.setSignLoanAmount(N2);
		//签约总还款额
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		//银行利息
		ywBusinessLoan.setBankRateAmount(A);
		ywBusinessLoan.setAmountMonthly(B1);
		ywBusinessLoan.setZhkcj(E);
		ywBusinessLoan.setQyhkcj(F);
		ywBusinessLoan.setServicePrice(X);
		ywBusinessLoan.setStagesFee(C1);
		ywBusinessLoan.setStagesFee2(C2);
		
		//首付和首付比例
		double firstPaymentAmount = NumberUtil.getBigDecimalDown(K0 - N0, 2, 2);
		ywBusinessLoan.setFirstPaymentAmount(firstPaymentAmount);
		ywBusinessLoan.setSfbl(NumberUtil.getBigDecimalUp(firstPaymentAmount / K0, 6, -1));
	}
	
	//计算平息
	public void calculationPx(AbstractYwBusinessLoan ywBusinessLoan) {
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 2, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//二手车评估价(四舍五入获取真实输入)
		double carEstimatePrice = ywBusinessLoan.getCarEstimatePrice() == null ? 0 : ywBusinessLoan.getCarEstimatePrice();
		double K1 = NumberUtil.getBigDecimalHalfUp(carEstimatePrice, 2, 2);
		ywBusinessLoan.setCarEstimatePrice(K1);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//签约公司利率
		double L3 = NumberUtil.getBigDecimalHalfUp(L0, 6, 6);
		ywBusinessLoan.setCompanyRate(L3);
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 2, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//按揭服务费
		double servicePrice = ywBusinessLoan.getServicePrice() == null ? 0 :ywBusinessLoan.getServicePrice().doubleValue();
		double X = NumberUtil.getBigDecimalHalfUp(servicePrice, 2, 2);
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		double N2 = NumberUtil.getBigDecimalDown(N0, 2, 2);
		double M1 = NumberUtil.getBigDecimalDown((N2 * (1 + L0)),2, 8);
		//银行利息(只舍不入)
		double A = NumberUtil.getBigDecimalDown(N2 * L0, 2, 8);
		//试算月还款
		double B0 = M1 / T;
		//签约月还款(取整 只入不舍)
		double B1 = NumberUtil.getBigDecimalUp(B0, 0, -1);
		
		double C1 = NumberUtil.getBigDecimalDown(N2 * L0, 2, 8);
		double C2 = C1;
		double E = 0;
		double F = 0;
		
		//车价
		if(K0 == 0.0) {
			//ExceptionHelper.thow("999", "二手车估价不能为0");
			E = 0;
			F = 0;
		} else  {
			E = NumberUtil.getBigDecimalHalfUp((M1 / K0), 6, -1);
			F = NumberUtil.getBigDecimalHalfUp((N2 / K0), 6, -1);
		}

		
		//银行利息
		ywBusinessLoan.setStagesFee(C1);
		ywBusinessLoan.setStagesFee2(C2);
		ywBusinessLoan.setSignLoanAmount(N2);
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		ywBusinessLoan.setBankRateAmount(A);
		ywBusinessLoan.setAmountMonthly(B1);
		ywBusinessLoan.setZhkcj(E);
		ywBusinessLoan.setQyhkcj(F);
		ywBusinessLoan.setServicePrice(X);
		
		//首付和首付比例
		double firstPaymentAmount = NumberUtil.getBigDecimalDown(K0 - N0, 2, 2);
		ywBusinessLoan.setFirstPaymentAmount(firstPaymentAmount);
		ywBusinessLoan.setSfbl(NumberUtil.getBigDecimalUp(firstPaymentAmount / K0, 6, -1));
	}
	
	//计算中国银行(整数值)
	public void calculationZgYhIntResult(AbstractYwBusinessLoan ywBusinessLoan){
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 2, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//二手车评估价(四舍五入获取真实输入)
		double carEstimatePrice = ywBusinessLoan.getCarEstimatePrice() == null ? 0 : ywBusinessLoan.getCarEstimatePrice();
		double K1 = NumberUtil.getBigDecimalHalfUp(carEstimatePrice, 2, 2);
		ywBusinessLoan.setCarEstimatePrice(K1);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 2, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//试算公司利率(四舍五入获取真实输入)
		String ccol2 = ywBusinessLoan.getCcol2();
		double cool2_ = StringUtils.isBlank(ccol2) ? 0 : Double.parseDouble(ccol2);
		ywBusinessLoan.setCcol2(new BigDecimal(cool2_).setScale(6, RoundingMode.HALF_UP).toString());
		
		//签约公司利率(只入不舍)
		double L3 = NumberUtil.getBigDecimalHalfUp(cool2_, 6, 6);
		ywBusinessLoan.setCompanyRate(L3);
		
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		//签约贷款额(只舍不入)		
		double N2 = NumberUtil.getBigDecimalDown(N0 * (1 + L3 - L0), 0, 8);
		//签约总还款额(只舍不入)
		double M1 = NumberUtil.getBigDecimalDown(N0 * (1 + L3), 0, 8);
		
		//签约每月还款额(只舍不入)
//		double R1 = NumberUtil.getBigDecimalDown(M1 / T, 0, -1);
		//签约首月还款额(只舍不入)
//		double R2 = NumberUtil.getBigDecimalDown(M1 - (T - 1) * R1, 0, 0);
		
		//签约每月本金还款额(只舍不入)
		double D1 = NumberUtil.getBigDecimalDown(N0 / T, 0, -1);
		//签约首月本金还款额(只舍不入)
		double D2 = NumberUtil.getBigDecimalDown(N0 - (T - 1) * D1, 0, 2);
		
		//签约手续费还款额1
		double S1 = NumberUtil.getBigDecimalDown(N0 * L3, 0, 8);
		//签约每月手续费还款额(只舍不入)
		double D3 = NumberUtil.getBigDecimalDown(S1 / T, 0, -1);
		//签约首月手续费还款额(只舍不入)
		double D4 = NumberUtil.getBigDecimalDown(S1 - (T - 1) * D3, 0, 0);
		
		//签约手续费还款额2
		double S2 = NumberUtil.getBigDecimalDown(N0 * L0, 2, 8);
		//签约每月手续费还款额(只舍不入)
		double D5 = NumberUtil.getBigDecimalDown(S2 / T, 2, -1);
		//签约首月手续费还款额(只舍不入)
		double D6 = NumberUtil.getBigDecimalDown(S2 - (T - 1) * D5, 2, 2);
		//银行利息
		double A = NumberUtil.getBigDecimalDown(N2 * L0, 0, 6);
		//按揭服务费
		double X = NumberUtil.getBigDecimalDown(N2 - N0, 2, 2);
				
		//银行利息
		ywBusinessLoan.setBankRateAmount(A);
		
		//签约贷款额(只舍不入)
		ywBusinessLoan.setSignLoanAmount(N2);
		//签约总还款额(只舍不入)
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		
		//签约每月还款额 amountMonthly
		ywBusinessLoan.setAmountMonthly(NumberUtil.getBigDecimalDown(D1 + D3, 0, 0));
		//签约首月还款额  firstMonthTotalAmount
		ywBusinessLoan.setFirstMonthTotalAmount(NumberUtil.getBigDecimalDown(D2 + D4, 0, 0));
		
		//签约每月本金还款额 monthlyPrincipalAmount
		ywBusinessLoan.setMonthlyPrincipalAmount(D1);
		//签约首月本金还款额  firstMonthAmount
		ywBusinessLoan.setFirstMonthAmount(D2);
		
		//签约手续费还款额1 stagesFee
		ywBusinessLoan.setStagesFee(S1);
		//签约每月手续费还款额1 monthlyFee
		ywBusinessLoan.setMonthlyFee(D3);
		//签约首月手续费还款额1 firstMonthFee
		ywBusinessLoan.setFirstMonthFee(D4);
		
		//签约手续费还款额1 stagesFee2
		ywBusinessLoan.setStagesFee2(S2);
		//签约每月手续费还款额1 monthlyFee2
		ywBusinessLoan.setMonthlyFee2(D5);
		//签约首月手续费还款额1 firstMonthFee2
		ywBusinessLoan.setFirstMonthFee2(D6);
		
		double E = 0;
		double F = 0;
		//新车
		if(K0 == 0.0) {
			//ExceptionHelper.thow("999", "二手车估价不能为0");
			E = 0;
			F = 0;
		} else {
			E = NumberUtil.getBigDecimalUp((M1 / K0), 6, -1);
			F = NumberUtil.getBigDecimalUp((N2 / K0), 6, -1);
		}
		ywBusinessLoan.setZhkcj(E);
		ywBusinessLoan.setQyhkcj(F);
		//按揭服务费
		ywBusinessLoan.setServicePrice(X);

		
		//首付和首付比例
		double firstPaymentAmount = NumberUtil.getBigDecimalDown(K0 - N0, 2, 2);
		ywBusinessLoan.setFirstPaymentAmount(firstPaymentAmount);
		ywBusinessLoan.setSfbl(NumberUtil.getBigDecimalUp(firstPaymentAmount / K0, 6, -1));
	}
	
	//计算平息
	public void calculationTjPx(AbstractYwBusinessLoan ywBusinessLoan) {
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 0, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//试算担保费率(只入不舍)
		Double L4 = 0d;
		ywBusinessLoan.setTrialAssureRate(L4);
		
		//合计利率(只入不舍)
		double L5 =  NumberUtil.getBigDecimalUp(L0 + 0, 6, 6);
		
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 0, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//首付金额
		double SF = NumberUtil.getBigDecimalUp(K0 - N0, 0, 2);
		ywBusinessLoan.setFirstPaymentAmount(SF);
		
		//试算总还款额（本息合计）
//		double M0 = NumberUtil.getBigDecimalUp(N0 * (1 + L5), 0, 8);
		
		//签约贷款额(申请分期额度)
//		Double signLoanAmount = ywBusinessLoan.getSignLoanAmount();
//		double N2 = NumberUtil.getBigDecimalHalfUp(signLoanAmount, 0, 2);
//		@update 2017-12-21 签约贷款额（申请分期额度）由 手工输入  改为 不能输入，并且值等于实际贷款额（分期金额）
		double N2 = N0;
		ywBusinessLoan.setSignLoanAmount(N0);
		
		//试算公司利率2
		double L2 = N2 * (1 + L0) / N0 - 1;
		//签约公司利率(只入不舍)
		double L3 = NumberUtil.getBigDecimalUp(L2, 6, -1);
		ywBusinessLoan.setCompanyRate(L3);
		
		//签约总还款额（实际本息合计）
		double M1 = NumberUtil.getBigDecimalDown(N2 * (1 + L0), 2, 6);
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		
		//按揭服务费
//		double X = NumberUtil.getBigDecimalUp(N2 - N0 , 0, 2);
//		@update 2017-12-21 按揭服务费  由（申请分期额度 - 分期金额） 改为手工输入
		Double servicePrice = ywBusinessLoan.getServicePrice();
		double X = NumberUtil.getBigDecimalHalfUp(servicePrice, 0, 2);
		ywBusinessLoan.setServicePrice(X);
		
		//试算月还款
		double B0 = NumberUtil.getBigDecimalDown(M1 / T , 2, -1);
		
		//签约月还款（月平均还款额）
		double B1  = NumberUtil.getBigDecimalUp(B0, 0, 2);
		ywBusinessLoan.setAmountMonthly(B1);
		
//		@update 2018-02-28 签约首月还款总额=签约总还款额（M1）-签约月还款（B1）*（贷款期限（T）-1）
		//签约首月还款总额
		ywBusinessLoan.setFirstMonthTotalAmount(NumberUtil.getBigDecimalUp(M1 - B1 * (T - 1), 0, 2));
		
		//分期手续费
		double C1 =  NumberUtil.getBigDecimalUp(N0 * L0, 0, 6);
		ywBusinessLoan.setBankRateAmount(C1);
		
		//担保服务费
		double DB = 0D;
		ywBusinessLoan.setAssureAmount(DB);
		
//		@update 2018-02-28 合计费用=实际贷款额（N0）*银行基准利率（L0）
		//合计费用
		double HJ = NumberUtil.getBigDecimalUp(N0 * L5, 0, 6);
		ywBusinessLoan.setHjAmount(HJ);
		
		//分期额度
		double FQED = NumberUtil.getBigDecimalUp(N0, 0, 0);
		ywBusinessLoan.setStageAmount(FQED);
		
		//分期比例 N0/K0[小数点2位，四舍五入]
//		@update 2017-12-21 分期比例 由 签约贷款额（申请分期额度）/车价  改为  实际贷款额（分期额度）/车价
//		double FQBL = NumberUtil.getBigDecimalUp(N2 / K0, 4, -1);
		double FQBL = NumberUtil.getBigDecimalUp(N0 / K0, 4, -1);
		ywBusinessLoan.setFqbl(FQBL);
		
		//首付比例 SF/K0[小数点2位，四舍五入]
		double SFBL = NumberUtil.getBigDecimalUp(SF / K0, 4, -1);
		ywBusinessLoan.setSfbl(SFBL);
		
		//总还款/车价
		double E = NumberUtil.getBigDecimalUp((M1 / K0), 6, -1);
		ywBusinessLoan.setZhkcj(E);
		//签约贷款额/车价
		double F = NumberUtil.getBigDecimalUp((N2 / K0), 6, -1);
		ywBusinessLoan.setQyhkcj(F);
	}
	
	//计算高息
	public void calculationTjGx(AbstractYwBusinessLoan ywBusinessLoan, SimpleDataDictionary intType, String addedValue2) {
		//车价(四舍五入获取真实输入)
		double carPrice = ywBusinessLoan.getCarPrice();
		double K0 = NumberUtil.getBigDecimalHalfUp(carPrice, 0, 2);
		ywBusinessLoan.setCarPrice(K0);
		
		//银行基准利率(只入不舍)
		double bankRate = ywBusinessLoan.getBankRate() == null ? 0 : ywBusinessLoan.getBankRate();
		double L0 = NumberUtil.getBigDecimalUp(bankRate, 6, 6);
		ywBusinessLoan.setBankRate(L0);
		
		//试算公司利率(四舍五入获取真实输入)-----试算合计利率
		String ccol2 = ywBusinessLoan.getCcol2();
		double cool2_ = StringUtils.isBlank(ccol2) ? 0 : Double.parseDouble(ccol2);
		ywBusinessLoan.setCcol2(new BigDecimal(cool2_).setScale(6, RoundingMode.HALF_UP).toString());
		
		//试算担保费率(只入不舍)
//		Double trialAssureRate = ywBusinessLoan.getTrialAssureRate() == null ? 0 : ywBusinessLoan.getTrialAssureRate();
//		double L4 = NumberUtil.getBigDecimalUp(trialAssureRate, 6, 6);
		double L4 = NumberUtil.getBigDecimalUp(cool2_ - L0, 6, 6);
		ywBusinessLoan.setTrialAssureRate(L4);
		
		//合计利率(只入不舍)
		double L5 =  NumberUtil.getBigDecimalUp(L0 + L4, 6, 6);
		
		//银行的贷款的期限
		int T = ywBusinessLoan.getLoanMonth();
		
		//实际贷款额(四舍五入获取真实输入)
		double loanAmount = ywBusinessLoan.getLoanAmount();
		double N0 = NumberUtil.getBigDecimalHalfUp(loanAmount, 0, 2);
		ywBusinessLoan.setLoanAmount(N0);
		
		//首付金额
		double SF = NumberUtil.getBigDecimalUp(K0 - N0, 0, 2);
		ywBusinessLoan.setFirstPaymentAmount(SF);
		
		//试算总还款额（本息合计）
		double M0 = NumberUtil.getBigDecimalUp(N0 * (1 + L5), 0, 8);
		
		//试算签约贷款额
		double N1 = NumberUtil.getBigDecimalUp(M0 / (1 + L0), 0, -1);
		
		String addedValue1 = intType.getAddedValue1();//取整模式
		if(StringUtils.isBlank(addedValue1)) {
			ExceptionHelper.thow("999","取整模式附加值1必须为一个数字");
		}
		int intValue = Integer.parseInt(addedValue1);
		boolean up = "1".equals(addedValue2); //向上取整
		BigDecimal N1_ = new BigDecimal(N1).setScale(2, RoundingMode.HALF_UP);
		BigDecimal nIntValue_ = N1_.divide(new BigDecimal(intValue));
		int nIntValue = nIntValue_.setScale(0, RoundingMode.DOWN).intValue();
		double additional =  N1_.compareTo(new BigDecimal(nIntValue * intValue)) > 0 ? (up ? intValue : 0) : 0;
		
		//签约贷款额(申请分期额度)
		double N2 =  NumberUtil.getBigDecimalDown((nIntValue * intValue) + additional, 0, 0); //[舍去小数点][intValue进位]
		ywBusinessLoan.setSignLoanAmount(N2);
		
		//试算公司利率2
		double L2 = N2 * (1 + L0) / N0 - 1;
		//签约公司利率(只入不舍)
		double L3 = NumberUtil.getBigDecimalUp(L2, 6, -1);
		ywBusinessLoan.setCompanyRate(L3);
		
		//签约总还款额（实际本息合计）
		double M1 = NumberUtil.getBigDecimalDown(N2 * (1 + L0), 2, 6);
		ywBusinessLoan.setTotalRepaymentAmount(M1);
		
		//按揭服务费
		double X = NumberUtil.getBigDecimalUp(N2 - N0 , 0, 2);
		ywBusinessLoan.setServicePrice(X);
		
//		@update 2018-02-28 试算月还款=签约总还款额（M1）/贷款期限（T）[小数点2位，只舍不入]
		//试算月还款
		double B0 = NumberUtil.getBigDecimalDown(M1 / T , 2, -1);

		//签约月还款（月平均还款额）
		double B1  = NumberUtil.getBigDecimalDown(B0, 0, 2);
		ywBusinessLoan.setAmountMonthly(B1);
		
//		@update 2018-02-28 签约首月还款总额=签约总还款额（M1）-签约月还款（B1）*（贷款期限（T）-1）
		//签约首月还款总额
		ywBusinessLoan.setFirstMonthTotalAmount(NumberUtil.getBigDecimalUp(M1 - B1 * (T - 1), 0, 2));
		
		//分期手续费
		double C1 =  NumberUtil.getBigDecimalUp(N0 * L0, 0, 6);
		ywBusinessLoan.setBankRateAmount(C1);
		
		//担保服务费
		double DB = NumberUtil.getBigDecimalUp(N0 * L4, 0, 6);
		ywBusinessLoan.setAssureAmount(DB);
		
//		@update 2018-02-28 合计费用=实际贷款额（N0）*合计费率（L1）
		//合计费用
		double HJ = NumberUtil.getBigDecimalUp(N0 * L5, 0, 6);
		ywBusinessLoan.setHjAmount(HJ);
		
		//分期额度
		double FQED = NumberUtil.getBigDecimalUp(N0 * (1 + L4), 0, 6);
		ywBusinessLoan.setStageAmount(FQED);
		
		//分期比例 N0/K0[小数点2位，四舍五入]
		double FQBL = NumberUtil.getBigDecimalUp(N0 / K0, 4, -1);
		ywBusinessLoan.setFqbl(FQBL);
		
		//首付比例 SF/K0[小数点2位，四舍五入]
		double SFBL = NumberUtil.getBigDecimalUp(SF / K0, 4, -1);
		ywBusinessLoan.setSfbl(SFBL);
		
		//总还款/车价
		double E = NumberUtil.getBigDecimalUp((M1 / K0), 6, -1);
		ywBusinessLoan.setZhkcj(E);
		//签约贷款额/车价
		double F = NumberUtil.getBigDecimalUp((N2 / K0), 6, -1);
		ywBusinessLoan.setQyhkcj(F);
		
	}
		
}
